package model.applicationmodule;


import model.applicationmodule.common.POMAppAM;

import oracle.adf.share.ADFContext;
import oracle.adf.share.security.SecurityContext;

import oracle.jbo.Row;
import oracle.jbo.RowSet;
import oracle.jbo.ViewCriteria;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Tue Oct 08 15:51:21 IST 2013
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class POMAppAMImpl extends ApplicationModuleImpl implements POMAppAM {
    /**
     * This is the default constructor (do not remove).
     */
    public POMAppAMImpl() {
    }

    /**
     * Container's getter for Projects.
     * @return Projects
     */
    public ViewObjectImpl getProjects() {
        return (ViewObjectImpl)findViewObject("Projects");
    }

    /**
     * Container's getter for PrjMembers.
     * @return PrjMembers
     */
    public ViewObjectImpl getPrjMembers() {
        return (ViewObjectImpl)findViewObject("PrjMembers");
    }

    /**
     * Container's getter for PrjTasks.
     * @return PrjTasks
     */
    public ViewObjectImpl getPrjTasks() {
        return (ViewObjectImpl)findViewObject("PrjTasks");
    }

    /**
     * Container's getter for Resources1.
     * @return Resources1
     */
    public ViewObjectImpl getResources() {
        return (ViewObjectImpl)findViewObject("Resources");
    }

    /**
     * Container's getter for LookupTypes.
     * @return LookupTypes
     */
    public ViewObjectImpl getLookupTypes() {
        return (ViewObjectImpl)findViewObject("LookupTypes");
    }

    /**
     * Container's getter for LookupValues.
     * @return LookupValues
     */
    public ViewObjectImpl getLookupValues() {
        return (ViewObjectImpl)findViewObject("LookupValues");
    }

    /**
     * Container's getter for ProjectsVOToPrjMembersVOVL1.
     * @return ProjectsVOToPrjMembersVOVL1
     */
    public ViewLinkImpl getProjectsVOToPrjMembersVOVL1() {
        return (ViewLinkImpl)findViewLink("ProjectsVOToPrjMembersVOVL1");
    }

    /**
     * Container's getter for ProjectsVOToPrjTasksVOVL1.
     * @return ProjectsVOToPrjTasksVOVL1
     */
    public ViewLinkImpl getProjectsVOToPrjTasksVOVL1() {
        return (ViewLinkImpl)findViewLink("ProjectsVOToPrjTasksVOVL1");
    }


    /**
     * Container's getter for Comments1.
     * @return Comments1
     */
    public ViewObjectImpl getComments() {
        return (ViewObjectImpl)findViewObject("Comments");
    }

    /**
     *
     * @param userName
     * @return rId
     */
    public Long getUserResourceId(String userName) {
        Long rId = null;
        ViewObjectImpl resourcesVO = getResources();
        resourcesVO.setApplyViewCriteriaName("findByUserNameVOCriteria");
        resourcesVO.setNamedWhereClauseParam("pUserName", userName);
        resourcesVO.executeQuery();
        Row r = resourcesVO.getRowAtRangeIndex(0);
        if(r!=null)
        {
//              rId = (Long)r.getAttribute("ResourceId");
      oracle.jbo.domain.Number num1=(oracle.jbo.domain.Number)r.getAttribute("ResourceId");
            rId=num1.longValue();
        
        }
        return rId;
    }

    /**
     *
     * @param resourceId
     */
    public void filterTask(Long resourceId) {
        if (resourceId != null) {
            ViewObjectImpl prjTaksVO = getPrjTasks();
            prjTaksVO.setApplyViewCriteriaName("findByResourceIdVOCriteria");
            prjTaksVO.setNamedWhereClauseParam("pResourceId", resourceId);
            prjTaksVO.executeQuery();
        }
    }

    /**
     *
     * @param resourceId
     */
    public void filterProjects(Long resourceId) {
        if (resourceId != null) {
            ViewObjectImpl projectsVO = getProjects();
            projectsVO.setApplyViewCriteriaName("searchByResourceIdVOCriteria");
            projectsVO.setNamedWhereClauseParam("pResourceId", resourceId);
            projectsVO.executeQuery();
        }
    }

    /**
     *
     * @param resourceId
     */
    public void filterMembers(Long resourceId) {
        String listofProjects = "(";

        if (resourceId != null) {
            ViewObjectImpl membersVO = getPrjMembersView();
            membersVO.setApplyViewCriteriaName("filterMembersByResourceIdVOCriteria");
            membersVO.setNamedWhereClauseParam("pResourceId", resourceId);
            membersVO.executeQuery();
            RowSet rs = membersVO.getRowSet();
            while (rs.hasNext()) {
                Row r = rs.next();
                if (r != null) {
                    Number prjId = (Number)r.getAttribute("PrjId");
                    listofProjects = listofProjects + prjId + ",";
                }

            }
            if (listofProjects.endsWith(",")) {
                listofProjects = listofProjects.substring(0, listofProjects.length() - 1);
            }
            listofProjects = listofProjects + ")";

            System.out.println("List of projects is "+listofProjects);
            if (!listofProjects.equals("()")) {
                String whereClause = null;
                whereClause = " PRJ_ID IN" + listofProjects;

                ViewObjectImpl vo = getProjects();
                vo.setWhereClause(whereClause);
                vo.executeQuery();
            }
        }
        else{
            //TODO 
        }
      
    }

    
   /**
     
     * @param sourceType
     */
    
    public void addComments(String sourceType) {

        SecurityContext securiContext = ADFContext.getCurrent().getSecurityContext();
        String userName = securiContext.getUserName();
        String comments = "";
        int sourceId = 0;
        int count = 0;

        if ("Members".equals(sourceType)) {
            ViewObjectImpl membersVO = getPrjMembers();
            Row membersRow = membersVO.getCurrentRow();
            Number memberId = (Number)membersRow.getAttribute("MemberId");
            sourceId = memberId.intValue();
            comments = (String)membersRow.getAttribute("Comments");
            membersRow.setAttribute("Comments", null);
            count++;
        } else if ("Projects".equals(sourceType)) {
            ViewObjectImpl projectsVO = getProjects();
            Row projectsRow = projectsVO.getCurrentRow();
            Number projectId = (Number)projectsRow.getAttribute("PrjId");
            sourceId = projectId.intValue();
            comments = (String)projectsRow.getAttribute("Comments");
            projectsRow.setAttribute("Comments", null);
            count++;
        } else if ("Tasks".equals(sourceType)) {
            ViewObjectImpl TasksVO = getPrjTasks();
            Row tasksRow = TasksVO.getCurrentRow();
            Number taskId = (Number)tasksRow.getAttribute("TaskId");
            sourceId = taskId.intValue();
            comments = (String)tasksRow.getAttribute("Comments");
            tasksRow.setAttribute("Comments", null);
            count++;
        }
        if (comments != null && count == 1) {
            ViewObjectImpl commentsVO = getComments();
            Row commentsRow = commentsVO.createRow();
            commentsRow.setAttribute("SourceType", sourceType);
            commentsRow.setAttribute("Comments", comments);
            commentsRow.setAttribute("SourceId", sourceId);
            commentsRow.setAttribute("CommentBy", userName);
            commentsVO.insertRow(commentsRow);
            commentsVO.getDBTransaction().commit();
        }
    }


    /**
     *
     * @param r
     */
    public void rollBackForTasks(Row r) {
        ViewObjectImpl prjTaksVO = getPrjTasks();
        r.remove();
        prjTaksVO.getDBTransaction().commit();
    }

    /**
     *
     * @param r
     */
    public void rollBackForProjects(Row r) {
        ViewObjectImpl prjVO = getProjects();
        r.remove();
        prjVO.getDBTransaction().commit();
    }

    /**
     *
     * @param r
     */
    public void rollBackForMembers(Row r) {
        ViewObjectImpl prjMembersVO = getPrjMembers();
        r.remove();
        prjMembersVO.getDBTransaction().commit();
    }


    /**
     *
     * @param sourceType
     */
    public void showComments(String sourceType, Number sourceId) {

        ViewObjectImpl commentsVO = getComments();
        ViewCriteria commentsVC = commentsVO.getViewCriteria("CommentsViewCriteria");
        commentsVO.applyViewCriteria(commentsVC);
        commentsVO.setNamedWhereClauseParam("pSourceType", sourceType);
        commentsVO.setNamedWhereClauseParam("pSourceId", sourceId);
        commentsVO.executeQuery();

    }


    /**
     * Container's getter for PrjMembersView.
     * @return PrjMembersView
     */
    public ViewObjectImpl getPrjMembersView() {
        return (ViewObjectImpl)findViewObject("PrjMembersView");
    }


    /**
     * Container's getter for ProjectNameReadOnly.
     * @return ProjectNameReadOnly
     */
    public ViewObjectImpl getProjectNameReadOnly() {
        return (ViewObjectImpl)findViewObject("ProjectNameReadOnly");
    }

    public void addMember() {

        ViewObjectImpl projectsVO = getProjects();
        ViewObjectImpl membersVO = getPrjMembers();

        if (projectsVO != null && membersVO != null) {
            Row projectsRow = projectsVO.getCurrentRow();
            if (projectsRow != null) {
                oracle.jbo.domain.Number resourceId =
                    (oracle.jbo.domain.Number)projectsRow.getAttribute("ProjectOwner");
                String role = "PRJ_ADMIN";

                Row membersRow = membersVO.createRow();
                if (membersRow != null) {
                    membersRow.setAttribute("ResourceId", resourceId);
                    membersRow.setAttribute("RoleCode", role);
                    membersVO.getDBTransaction().commit();
                }
            }
        }
    }


    /**
     * Container's getter for ResourceVA1.
     * @return ResourceVA1
     */
    public ViewObjectImpl getResourceVA() {
        return (ViewObjectImpl)findViewObject("ResourceVA");
    }
}
